package xjtu.sad.zbsj.statistics.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xjtu.sad.zbsj.common.Utils;
import xjtu.sad.zbsj.statistics.*;
import xjtu.sad.zbsj.statistics.dao.StatisticsDao;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private StatisticsDao statisticsDao;

    @Override
    public void withdrawMoney(StatisticsDto statisticsDto) {
        statisticsDao.withdrawMoney(statisticsDto);
        int withdrawId;
        try{
            withdrawId=statisticsDao.getWithdraw_id()+1;
        }catch(Exception e){
            withdrawId = 1;
        }
        System.out.println(withdrawId);
        statisticsDto.setWithdraw_time(Utils.getNow());
        System.out.println(Utils.getNow());
        statisticsDto.setWithdraw_id(withdrawId);
        statisticsDao.record(statisticsDto);
    }

    @Override
    public String getCredit(String user_id) {
        return statisticsDao.getCredit(user_id);
    }

    @Override
    public MonthNum selectTaskNum(String user_id,String year) {
        List<String> end_times = statisticsDao.selectTaskNums(user_id,year);
        MonthNum monthNum = new MonthNum();
        String [] yearAndMonth;
        int jan=0,feb=0,mar=0,apr=0,may=0,jun=0,jul=0,aug=0,sep=0,oct=0,nov=0,dec=0;
        for(String time : end_times){
            yearAndMonth = time.split("-");
            switch(yearAndMonth[1]){
                case "1":jan++;break;
                case "2":feb++;break;
                case "3":mar++;break;
                case "4":apr++;break;
                case "5":may++;break;
                case "6":jun++;break;
                case "7":jul++;break;
                case "8":aug++;break;
                case "9":sep++;break;
                case "10":oct++;break;
                case "11":nov++;break;
                case "12":dec++;break;
            }
        }
        monthNum.setJan(jan);
        monthNum.setFeb(feb);
        monthNum.setMar(mar);
        monthNum.setApr(apr);
        monthNum.setMay(may);
        monthNum.setJun(jun);
        monthNum.setJul(jul);
        monthNum.setAug(aug);
        monthNum.setSep(sep);
        monthNum.setOct(oct);
        monthNum.setNov(nov);
        monthNum.setDec(dec);
        return monthNum;
    }

    @Override
    public MonthNum selectTaskSNum(String user_id,String year) {
        List<String> start_times = statisticsDao.selectTaskSNums(user_id,year);
        MonthNum monthNum = new MonthNum();
        String [] yearAndMonth;
        int jan=0,feb=0,mar=0,apr=0,may=0,jun=0,jul=0,aug=0,sep=0,oct=0,nov=0,dec=0;
        for(String time : start_times){
            yearAndMonth = time.split("-");
            switch(yearAndMonth[1]){
                case "1":jan++;break;
                case "2":feb++;break;
                case "3":mar++;break;
                case "4":apr++;break;
                case "5":may++;break;
                case "6":jun++;break;
                case "7":jul++;break;
                case "8":aug++;break;
                case "9":sep++;break;
                case "10":oct++;break;
                case "11":nov++;break;
                case "12":dec++;break;
            }
        }
        monthNum.setJan(jan);
        monthNum.setFeb(feb);
        monthNum.setMar(mar);
        monthNum.setApr(apr);
        monthNum.setMay(may);
        monthNum.setJun(jun);
        monthNum.setJul(jul);
        monthNum.setAug(aug);
        monthNum.setSep(sep);
        monthNum.setOct(oct);
        monthNum.setNov(nov);
        monthNum.setDec(dec);
        return monthNum;
    }

    @Override
    public List<Float> selectRightRate(String user_id) {
        List<Float> rates = statisticsDao.selectRightRate(user_id);
        return rates;
    }

    @Override
    public float selectTotalRightRate(String user_id) {
        return statisticsDao.selectTotalRightRate(user_id);
    }

    @Override
    public int getCount(String user_id) {
        return statisticsDao.getCount(user_id);
    }

    @Override
    public List<WithdrawHistory> getHistory(String user_id) {
        return statisticsDao.getHistory(user_id);
    }


}
